﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using MySql.Data.MySqlClient;

namespace MakeIT.MiraPark.Negocios
{
    public static partial class ModelBO
    {

        public static TipoColisaoVO getTipoColisao(int idColisao)
        {
            List<TipoColisaoVO> listTipoColisaoVO = getTipoColisoes(false, idColisao);
            if (listTipoColisaoVO != null && listTipoColisaoVO.Count > 0)
                return listTipoColisaoVO[0];
            else
                return null;
        }

        public static List<TipoColisaoVO> getTipoColisoes()
        {
            List<TipoColisaoVO> tipoColisoes = getTipoColisoes(false);
            return tipoColisoes;
        }

        public static List<TipoColisaoVO> getTipoColisoes(bool somentePermitidos)
        {
            return getTipoColisoes(somentePermitidos, null);
        }
       
        public static List<TipoColisaoVO> getTipoColisoes(bool somentePermitidos, int? idColisao)
        {
            string where = "WHERE ";
            StringBuilder query = new StringBuilder();

            query.AppendLine("SELECT IdColisao, Colisao, Permitido, AvariaX, AvariaY, AvariaWidth, AvariaHeight ");
            query.AppendLine("FROM tipocolisao ");
            if (somentePermitidos)
            {
                query.AppendLine(where + "Permitido=?Permitido ");
                where = "AND ";
            }
            if (idColisao != null)
            {
                query.AppendLine(where + "IdColisao=?IdColisao ");
                where = "AND ";
            }
            query.AppendLine("ORDER BY Colisao ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            if (somentePermitidos) cmd.Parameters.AddWithValue("?Permitido", somentePermitidos);
            if (idColisao != null) cmd.Parameters.AddWithValue("?IdColisao", idColisao);

            MySqlDataReader reader = null;
            try
            {
                cmd.Connection.Open();

                reader = cmd.ExecuteReader();

                List<TipoColisaoVO> listTipoColisao = new List<TipoColisaoVO>();

                while (reader.Read())
                {
                    TipoColisaoVO tipoColisaoVO = TipoColisaoVO.CreateTipoColisaoVO();
                    tipoColisaoVO.idTipoColisao = reader.GetInt32("IdColisao");
                    tipoColisaoVO.colisao = reader.GetString("Colisao");
                    tipoColisaoVO.permitido = reader.GetBoolean("Permitido");
                    tipoColisaoVO.avariaX = reader.GetInt32("AvariaX");
                    tipoColisaoVO.avariaY = reader.GetInt32("AvariaY");
                    tipoColisaoVO.avariaWidth = reader.GetInt32("AvariaWidth");
                    tipoColisaoVO.avariaHeight = reader.GetInt32("AvariaHeight");
                   
                    listTipoColisao.Add(tipoColisaoVO);
                }
                reader.Close();
                return listTipoColisao;
            }
            catch (Exception ex){ throw ex; }
            finally { cmd.Connection.Close(); }
        }

        public static void setTipoColisao(TipoColisaoVO tipoColisaoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("UPDATE tipocolisao ");
            query.AppendLine("SET Colisao=?Colisao, Permitido=?Permitido, AvariaX=?AvariaX, AvariaY=?AvariaY, AvariaWidth=?AvariaWidth, AvariaHeight=?AvariaHeight ");
            query.AppendLine("WHERE IdColisao=?IdColisao ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdColisao", tipoColisaoVO.idTipoColisao);
            cmd.Parameters.AddWithValue("?Colisao", tipoColisaoVO.colisao);
            cmd.Parameters.AddWithValue("?Permitido", tipoColisaoVO.permitido);
            cmd.Parameters.AddWithValue("?AvariaX", tipoColisaoVO.avariaX);
            cmd.Parameters.AddWithValue("?AvariaY", tipoColisaoVO.avariaY);
            cmd.Parameters.AddWithValue("?AvariaWidth", tipoColisaoVO.avariaWidth);
            cmd.Parameters.AddWithValue("?AvariaHeight", tipoColisaoVO.avariaHeight);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static long newTipoColisao(TipoColisaoVO tipoColisaoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("INSERT INTO tipocolisao ");
            query.AppendLine("SET Colisao=?Colisao, Permitido=?Permitido, AvariaX=?AvariaX, AvariaY=?AvariaY, AvariaWidth=?AvariaWidth, AvariaHeight=?AvariaHeight ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?Colisao", tipoColisaoVO.colisao);
            cmd.Parameters.AddWithValue("?Permitido", tipoColisaoVO.permitido);
            cmd.Parameters.AddWithValue("?AvariaX", tipoColisaoVO.avariaX);
            cmd.Parameters.AddWithValue("?AvariaY", tipoColisaoVO.avariaY);
            cmd.Parameters.AddWithValue("?AvariaWidth", tipoColisaoVO.avariaWidth);
            cmd.Parameters.AddWithValue("?AvariaHeight", tipoColisaoVO.avariaHeight);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
                return cmd.LastInsertedId;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static void delTipoColisao(TipoColisaoVO tipoColisaoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("DELETE FROM tipocolisao ");
            query.AppendLine("WHERE IdColisao=?IdColisao ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdColisao", tipoColisaoVO.idTipoColisao);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static bool readyToDeleteTipoColisao(int idTipoColisao)
        {
            int contagem = 0;

            MySqlDataReader reader = null;
            StringBuilder query = new StringBuilder();
            MySqlCommand cmd = getConnection();
            cmd.Connection.Open();

            try
            {

                query.AppendLine("SELECT count(*) contagem FROM veiculoxavaria WHERE IdColisao=?IdColisao ");
                cmd.CommandText = query.ToString();
                cmd.Parameters.AddWithValue("?IdColisao", idTipoColisao);
                reader = cmd.ExecuteReader();
                reader.Read();
                contagem = reader.GetInt32("contagem");
                reader.Close();

            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

            return contagem == 0;

        }
    
    }
}
